package com.atguigu.eduservice.controller.admin;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ResultCode;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Mr.Alan
 * @since 2020-08-16
 */
@RestController
@RequestMapping("/eduService/chapter")
@CrossOrigin
public class EduChapterController {

    @Resource
    private EduChapterService eduChapterService;
    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private VodClient vodClient;

    @ApiOperation("删除章节下的视频")
    @PostMapping("/vod/deleteOne/{id}")
    public R deleteVideoById(@PathVariable("id") String id) {
        EduChapter chapter = eduChapterService.lambdaQuery().eq(EduChapter::getId, id).select(EduChapter::getId, EduChapter::getVideoId).one();
        if (ObjectUtils.isNotNull(chapter)) {
            R r = vodClient.deleteVideoById(chapter.getVideoId());
            if (r.getCode().equals(ResultCode.SUCCESS)) {
                chapter.setVideoId("");
                eduChapterService.updateById(chapter);
            }
        }
        return R.ok();
    }

    @ApiOperation(value = "Excel批量导入")
    @PostMapping("chapterImport/{id}")
    public R addSubject(@PathVariable("id") String id, MultipartFile file) {
        eduChapterService.importChapterData(file, eduChapterService, id);
        eduCourseService.offShelfCourse(id);
        //判断返回集合是否为空
        return R.ok();
    }

    @ApiOperation("通过课程Id查询该课程的所有章节信息")
    @PostMapping("/list/{id}")
    public R listByCourseId(@PathVariable("id") String id) {

        List<EduChapter> list = eduChapterService.listByCourseId(id);
        if (CollectionUtils.isNotEmpty(list)) {
            return R.ok().data("list", list);
        }
        return R.ok().data("list", Collections.emptyList());

    }

    @ApiOperation("添加章节")
    @PostMapping("/save")
    public R save(@RequestBody EduChapter eduChapter) {

        eduChapter.setGmtCreate(new Date()).setGmtModified(new Date());
        if (eduChapterService.save(eduChapter)) {
            eduCourseService.offShelfCourse(eduChapter.getCourseId());
            return R.ok();
        }
        return R.error();

    }

    @ApiOperation("修改章节")
    @PostMapping("/update")
    public R update(@RequestBody EduChapter eduChapter) {

        eduChapter.setGmtModified(new Date());
        if (eduChapterService.updateById(eduChapter)) {
            eduCourseService.offShelfCourse(eduChapter.getCourseId());
            return R.ok();
        }
        return R.error();

    }

    @ApiOperation("删除章节")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {

        eduCourseService.offShelfCourseByChapterId(id);
        if (eduChapterService.removeChapterById(id)) {
            return R.ok();
        }
        return R.error();

    }

    @ApiOperation("删除全部章节")
    @PostMapping("/delete/all/{id}")
    public R deleteAll(@PathVariable("id") String id) {

        if (eduChapterService.removeAll(id)) {
            return R.ok();
        }
        return R.error();

    }


}

